//
//  GraphCharView.swift
//  summaryGraph
//
//  Created by Ezio on 2017/2/21.
//  Copyright © 2017年 飞天江郎. All rights reserved.
//

import UIKit

@IBDesignable class GraphCharView: UIView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate{

    @IBOutlet weak var graphCollection: UICollectionView!
    @IBOutlet weak var dayLab: UILabel!
    
    var heightCell:Int!
    
    override func layoutSubviews() {
        super.layoutSubviews()
        

    }
    
    //初始化默认设置
    func initialSetup(){
        
        graphCollection.delegate = self
        graphCollection.dataSource = self
        graphCollection.contentInset = UIEdgeInsets.init(top: 0, left: UIScreen.main.bounds.size.width/2-10, bottom: 0, right: UIScreen.main.bounds.size.width/2-10)
        // 从nib中注册cell，适用于通过xib创建cell的情况；如果是通过代码创建的cell，则使用registerClass方法
        graphCollection.register(UINib(nibName: "GraphCharCollectionViewCell", bundle: nil), forCellWithReuseIdentifier:"graphCell")
        
        
        heightCell = 0
    }
    
    /*---这里是collectionViewFlowLayout的Delegate---*/
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: 30, height: 250)
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0.5
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
    

    /* ------- 这里是collectionView的Delegate ------- */
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        heightCell = indexPath.row
        graphCollection.reloadData()
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "graphCell", for: indexPath) as! GraphCharCollectionViewCell
        
        cell.firstImgHeight.constant = CGFloat(50)
        cell.secondImgHeight.constant = CGFloat(15)
        cell.thirdImgHeight.constant = CGFloat(70)
        cell.daysLab.text = "02/22"
        
        cell.lineViewHeight.constant = cell.firstImgHeight.constant+cell.secondImgHeight.constant+cell.thirdImgHeight.constant
        
        if heightCell == indexPath.row{
            cell.lineView.layer.borderColor = UIColor.white.cgColor
            cell.lineView.layer.borderWidth = 1
        }else{
            cell.lineView.layer.borderColor = UIColor.clear.cgColor
            cell.lineView.layer.borderWidth = 1
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 20
        
    }
    
   
    
    /*------ 这里是ScrollViewDelegate ------*/
    // variable to save the last position visited, default to zero
    private var lastContentOffset: CGFloat = 0
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
//        

        
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
     
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        print("scrollViewDidEndDecelerating")
        self.lastContentOffset = scrollView.contentOffset.x
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
//        if (self.lastContentOffset > scrollView.contentOffset.x) {
//            // move up
//            print("move left:\(self.lastContentOffset-scrollView.contentOffset.x)）")
//        }
//        else if (self.lastContentOffset < scrollView.contentOffset.x) {
//            // move down
//            print("move right:\(scrollView.contentOffset.x-self.lastContentOffset)")
//        }
        
        // update the new position acquired
//        self.lastContentOffset = scrollView.contentOffset.x
        
    }
    
    
    
    
    
    
    
    /*** 下面的几个方法都是为了让这个自定义类能将xib里的view加载进来。这个是通用的，我们不需修改。 ****/
    var contentView:UIView!
    
    //初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        addSubview(contentView)
        addConstraints()
        //初始化属性配置
        initialSetup()
    }
    
    //初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        contentView = loadViewFromNib()
        addSubview(contentView)
        addConstraints()
        //初始化属性配置
        initialSetup()
    }
    //加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }
    //设置好xib视图约束
    func addConstraints() {
        contentView.translatesAutoresizingMaskIntoConstraints = false
        var constraint = NSLayoutConstraint(item: contentView, attribute: .leading,
                                            relatedBy: .equal, toItem: self, attribute: .leading,
                                            multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .trailing,
                                        relatedBy: .equal, toItem: self, attribute: .trailing,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal,
                                        toItem: self, attribute: .top, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .bottom,
                                        relatedBy: .equal, toItem: self, attribute: .bottom,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
    }
    

}
